package cn.tedu.ivosback.application.service.impl;

import cn.tedu.ivosback.application.mapper.ApplicationMapper;
import cn.tedu.ivosback.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivosback.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivosback.application.pojo.entity.Application;
import cn.tedu.ivosback.application.pojo.vo.ApplicationVO;
import cn.tedu.ivosback.application.service.ApplicationService;
import cn.tedu.ivosback.audit.pojo.vo.AuditVO;
import cn.tedu.ivosback.audit.service.AuditService;
import cn.tedu.ivosback.base.enums.ApplicationStatusEnum;
import cn.tedu.ivosback.user.pojo.vo.UserVO;
import cn.tedu.ivosback.user.service.UserService;
import cn.tedu.ivosback.vehicle.mapper.VehicleMapper;
import cn.tedu.ivosback.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    ApplicationMapper applicationMapper;
    @Autowired
    AuditService auditService;
    @Autowired
    UserService userService;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请:applicationSaveParam={}", applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam, application);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        applicationMapper.insertApplication(application);
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery) {
        log.debug("查询申请:applicationQuery={}", applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        for (int i = 0; i < list.size(); i++) {
            ApplicationVO applicationVO = list.get(i);
            assignAuditUserList(applicationVO);
        }
        return list;
    }

    public void assignAuditUserList(ApplicationVO applicationVO) {
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList =
                auditService.selectAuditByApplicationId(applicationVO.getId());
        for (int i = 0; i < auditVOList.size(); i++) {
            auditUserIdList.add(auditVOList.get(i).getAuditUserId());
            UserVO userVO = userService.selectById(auditVOList.get(i).getAuditUserId());
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String auditUsername : auditUsernameList) {
            stringJoiner.add(auditUsername);
        }
        applicationVO.setAuditUsernameList(stringJoiner.toString());
    }

    @Override
    public void updateApplication(Long applicationId) {
        log.debug("删除申请:applicationId={}", applicationId);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.updateApplication(application);
        auditService.deleteAuditByApplicationId(applicationId);
    }

    @Override
    public void distribute(Long vehicleId, Long applicationId) {
        log.debug("分配车辆:vehicleId={},applicationId={}", vehicleId, applicationId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        applicationMapper.updateApplication(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("归还车辆,id={},vehicleId={}", applicationId, vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setUpdateTime(new Date());
        application.setStatus(ApplicationStatusEnum.END.getCode());
        applicationMapper.updateApplication(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicleMapper.update(vehicle);
    }
}
